package by.alex.dzeshko.facebooksuperclient.providers;

import org.json.JSONException;
import org.json.JSONObject;
import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.util.Log;
import by.alex.dzeshko.facebooksuperclient.R;
import by.alex.dzeshko.facebooksuperclient.database.DbNewsfeedHelper;
import by.alex.dzeshko.facebooksuperclient.providers.contract.FbContract;

public class FbContentProvider extends ContentProvider {
	
	private static final String LOG_TAG = "FbContentProvider";
	private DbNewsfeedHelper dbManager;

	@Override
	public int delete(Uri arg0, String arg1, String[] arg2) {
		return 0;
	}

	@Override
	public String getType(Uri arg0) {		
		return FbContract.FeedColumns.CONTENT_TYPE;
	}

	@Override
	public Uri insert(Uri uri, ContentValues vals) {
		String jsonData = vals.getAsString("data");
		JSONObject jsonObject;
		try {
			jsonObject = new JSONObject(jsonData);
			long id = dbManager.addItem(jsonObject);
			Uri itemUri = ContentUris.withAppendedId(FbContract.FeedColumns.CONTENT_URI, id);
			getContext().getContentResolver().notifyChange(itemUri, null);
			return itemUri;
		} catch (JSONException e) {
			e.printStackTrace();
			Log.e(LOG_TAG, e.toString());
		}
		return null;
		
	}

	@Override
	public boolean onCreate() {
		dbManager = new DbNewsfeedHelper(getContext(), null);
		return true;
	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection,	String[] selectionArgs, String sortOrder) {
		Cursor items = dbManager.getItems(selection);
		items.setNotificationUri(getContext().getContentResolver(), uri);
		return items;
	}

	@Override
	public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
		
		return 0;
	}
}
	/*	class ThreadedDbLoader implements Runnable{
		
		Object sync;
		
		JSONObject jsonObject;
		
		IdContainer idContainer;
		
		ThreadedDbLoader(Object sync, JSONObject jsonObject, IdContainer idContainer){
			this.sync = sync;
			this.jsonObject = jsonObject;
			this.idContainer = idContainer;
		}

		@Override
		public void run() {
			synchronized (sync) {
				Log.d("thread.run","start");
				idContainer.setId(dbManager.addItem(jsonObject));
				sync.notify();
			}
		}
		
	}
	
	 static class IdContainer{
		
		private long id;

		IdContainer(long id){
			this.id = id;
		}
		
		void setId(long id){
			Log.d("setId","value: "+id);
			this.id = id;
		}
		
		long getId(){
			Log.d("getId","value: "+id);
			return id;
		}
	}
}

	@Override
public Uri insert(Uri uri, ContentValues vals) {
	String jsonData = vals.getAsString("data");
	JSONObject jsonObject = null;
	Uri itemUri = null;
	Object sync = new Object();
	IdContainer idContainer = new IdContainer(-2);
	try {
		jsonObject = new JSONObject(jsonData);
	} catch (JSONException e) {
		e.printStackTrace();
		Log.e("insert C-Pr ERROR", e.getMessage());
	}
	Thread thread = new Thread(new ThreadedDbLoader(sync, jsonObject, idContainer));
	thread.start();
	synchronized (sync) {
		Log.d("insert.syncro","start");
		while(idContainer.getId()==-2){
			try {
				Log.d("insert.syncro","wait");
				sync.wait(200);
			} catch (InterruptedException e) {
				Log.e("FbContentProvider",e.toString());
				e.printStackTrace();
			}
		}
		Log.d("insert.syncro","after while");
		itemUri = ContentUris.withAppendedId(FbContract.FeedColumns.CONTENT_URI, idContainer.getId());
		getContext().getContentResolver().notifyChange(itemUri, null);
		return itemUri;
	}
}
*/	
